<template>
  <div style="padding: 10px 0px; background-color: white;">
    <van-form @submit="submitForm">
      <van-field
        :value="$store.getters.name"
        label="姓名"
        placeholder="提交人姓名"
        readonly
      />
      <van-field
        v-model="lastWeekData.lastWeekPlanned"
        rows="5"
        autosize
        label="上周计划"
        type="textarea"
        readonly
        placeholder="您上周没有填写计划"
      />
      <van-field
        v-model="form.weeklyContent"
        rows="5"
        autosize
        label="本周计划"
        type="textarea"
        placeholder="请输入本周计划"
        :rules="[{ required: true, message: '请输入本周计划' }]"
      />
      <van-divider>下周计划</van-divider>
      <van-field
        v-model="form.day1"
        rows="3"
        autosize
        label="周一"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day2"
        rows="3"
        autosize
        label="周二"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day3"
        rows="3"
        autosize
        label="周三"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day4"
        rows="3"
        autosize
        label="周四"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day5"
        rows="3"
        autosize
        label="周五"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day6"
        rows="3"
        autosize
        label="周六"
        type="textarea"
        placeholder="请输入内容"
      />
      <van-field
        v-model="form.day7"
        rows="3"
        autosize
        label="周日"
        type="textarea"
        placeholder="请输入内容"
      />
      <searchable-picker
        v-model="form.shareList"
        :multiSelect="true"
        :columns="options"
        labelKey="userName"
        valueKey="userId"
        label="分享人列表"
        placeholder="选择分享人"
        :rules="[{ required: true, message: '请选择分享人' }]"
      />
      <div
        style="
          margin-bottom: 50px;
          padding: 15px;
          font-size: 12px;
          line-height: 20px;
        "
      >
        <span style="color: red">温馨提示：</span>
        <div>
          分享人请选择：卢小安，熊军凯，黄红平，朱小琴，刘宁，刘珂暘，黄宪平，丰雅伟，龚志伟，金静及本部门同事和与之相关业务的同事
        </div>
      </div>
      <div class="mobile-footer">
        <van-button  
          native-type="submit"
          style="width: 49%; border-radius: 10px"
          @click="weeklyStatus = '0'"
          >暂存</van-button
        >
        <van-button
          
          type="info"
          native-type="submit"
          style="width: 49%; border-radius: 10px"
          @click="weeklyStatus = '1'"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { addWeekly, getLatestWeekly } from "@/api/erp/weekly";

import { getUserListByStatus } from "@/api/system/user";

export default {
  name: "WeeklyAdd",
  data() {
    return {
      // 周报数据表格数据
      weeklyList: [],
      // 表单参数
      form: {
        weeklyId: null,
        weeklyContent: null,
        day1: null,
        day2: null,
        day3: null,
        day4: null,
        day5: null,
        day6: null,
        day7: null,
        shareList: [],
        submitTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        weeklyStatus: null,
        delFlag: null,
      },
      lastWeekData: {},
      options: [],
      weeklyStatus: "0",
    };
  },
  created() {
    this.getWeeklyInfo();
    this.getLatestWeekly();
    this.getUserListByStatus();
  },
  methods: {
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.options = res.data;
      });
    },
    getWeeklyInfo() {
      getLatestWeekly({ weeklyStatus: "0" }).then((res) => {
        if (res.data) {
          this.form = res.data;
          this.form.shareList = res.data.shareList ? res.data.shareList.split(",") : null;
        }
      });
    },
    getLatestWeekly() {
      getLatestWeekly({ weeklyStatus: "1" }).then((res) => {
        if (res.data) {
          this.lastWeekData = res.data;
          this.form.shareList = res.data.shareList ? res.data.shareList.split(",") : null;
          this.lastWeekData.lastWeekPlanned =
            "1." +
            (res.data.day1 || "今日无计划") +
            "\n" +
            "2." +
            (res.data.day2 || "今日无计划") +
            "\n" +
            "3." +
            (res.data.day3 || "今日无计划") +
            "\n" +
            "4." +
            (res.data.day4 || "今日无计划") +
            "\n" +
            "5." +
            (res.data.day5 || "今日无计划") +
            "\n" +
            "6." +
            (res.data.day6 || "今日无计划") +
            "\n" +
            "7." +
            (res.data.day7 || "今日无计划");
        } else {
          if (!this.form.shareList || this.form.shareList.length === 0) {
            this.form.shareList = [
              "65315650692447",
              "113030515728601022",
              "090157122440088021",
              "071328165826147062",
              "1755536800674921",
              "041311204721141294",
              "012227523739810509",
              "01083459565620452138",
              "0345446612759419865",
              "66545605321195944",
            ];
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.weeklyStatus === "1") {
        this.$dialog.confirm({
          title: "周报提交提示",
          message: "提交后将无法修改, 是否继续?",
        }).then(() => {
          this.submit(this.weeklyStatus);
        });
      } else {
        this.submit(this.weeklyStatus);
      }
    },
    submit(weeklyStatus) {
      this.form.weeklyStatus = weeklyStatus;
      this.form.shareList = this.form.shareList?.join(",");
      this.form.deviceFlag = '1' // 移动端标识
      addWeekly(this.form).then((response) => {
        this.$toast.success("提交成功");
        this.visible = false;
        this.$router.replace({
          path: "/erp/weeklyList",
        });
      });
    },
  },
};
</script>

<style scoped>
.submitButton {
  width: 95%;
  height: 50px;
  position: fixed;
  top: 90%;
  left: 10px;
}
</style>